package com.dev.bbs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dev.bbs.domain.BbsUserCollect;
import com.dev.bbs.mapper.BbsUserCollectMapper;
import com.dev.bbs.service.IBbsUserCollectService;
import com.dev.common.exception.BusinessException;
import com.dev.common.utils.DateUtils;

import tk.mybatis.mapper.entity.Example;

/**
 * 用户文章收藏Service业务层处理
 * 
 * @author dev
 * @date 2019-09-23
 */
@Service
public class BbsUserCollectServiceImpl implements IBbsUserCollectService
{
    @Autowired
    private BbsUserCollectMapper bbsUserCollectMapper;

    /**
     * 查询用户文章收藏
     * 
     * @param collectId 用户文章收藏ID
     * @return 用户文章收藏
     */
    @Override
    public BbsUserCollect selectBbsUserCollectById(Long collectId)
    {
        return bbsUserCollectMapper.selectByPrimaryKey(collectId);
    }

    /**
     * 查询用户文章收藏列表
     * 
     * @param bbsUserCollect 用户文章收藏
     * @return 用户文章收藏
     */
    @Override
    public List<BbsUserCollect> selectBbsUserCollectList(BbsUserCollect bbsUserCollect)
    {
        return bbsUserCollectMapper.select(bbsUserCollect);
    }

    /**
     * 新增用户文章收藏
     * 
     * @param bbsUserCollect 用户文章收藏
     * @return 结果
     */
    @Override
    public int insertBbsUserCollect(BbsUserCollect bbsUserCollect)
    {
        if (collected(bbsUserCollect.getUserId(), bbsUserCollect.getArtId()))
        {
            throw new BusinessException("已经收藏过啦~~~");
        }
        bbsUserCollect.setCreateTime(DateUtils.getNowDate());
        return bbsUserCollectMapper.insertSelective(bbsUserCollect);
    }

    /**
     * 修改用户文章收藏
     * 
     * @param bbsUserCollect 用户文章收藏
     * @return 结果
     */
    @Override
    public int updateBbsUserCollect(BbsUserCollect bbsUserCollect)
    {
        bbsUserCollect.setUpdateTime(DateUtils.getNowDate());
        return bbsUserCollectMapper.updateByPrimaryKeySelective(bbsUserCollect);
    }

    /**
     * 删除用户文章收藏对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBbsUserCollectByIds(String ids)
    {
        return bbsUserCollectMapper.deleteByIds(ids);
    }

    /**
     * 删除用户文章收藏信息
     * 
     * @param collectId 用户文章收藏ID
     * @return 结果
     */
    @Override
    public int deleteBbsUserCollectById(long collectId)
    {
        return bbsUserCollectMapper.deleteByPrimaryKey(collectId);
    }

    /**
     * 检查是否已经收藏
     * @param userId 用户编号
     * @param artId 帖子编号
     * @return
     * @author zmr
     */
    @Override
    public boolean collected(long userId, long artId)
    {
        Example example = new Example(BbsUserCollect.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("artId", artId);
        return bbsUserCollectMapper.selectCountByExample(example) > 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.dev.bbs.service.IBbsUserCollectService#uncollect(long, long)
     */
    @Override
    public boolean uncollect(long userId, long artId)
    {
        Example example = new Example(BbsUserCollect.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("artId", artId);
        return bbsUserCollectMapper.deleteByExample(example) > 0;
    }
}
